source("script_02_create_objects_for_graphs.R")
library(foreign)
library(plyr)

#get n.sims from other script
load("n.sims.image")
#read in data--at nominee level
nomd <- read.dta("simulated_nom_level_data_from_r_new.dta", convert.underscore=T)
head(nomd)
nomd <- nomd[order(nomd$nominee, nomd$simulation),]

#################
#DESCRIPTIVE PLOTS
########################
##############################################
#plot just nominees' ideal points, with uncertainty
##############################################
axis.text <- 1.2
title.text <- 1.4
point.size <- 1.5
lo.ci <- .05
hi.ci <- .95

#DW
pdf("Figure_A1_left.pdf", height=8, width=6)

x.lim<-c(-.6,.6)
nom.ideal.quantile.dw <- matrix( unlist ( tapply(nomd$nom.proj.sim.dw, nomd$nominee, function(x) quantile(x, probs=c(lo.ci, .5,.95))) ), ncol=3, byrow=T)

nom.ideal.quantile.dw.sorted <- nom.ideal.quantile.dw[order( nom.ideal.quantile.dw[,2]),]
sort.nominee.title.dw <- nominee.title.dw[order( nom.ideal.quantile.dw[,2])]
y <- c(1:nrow(nom.ideal.quantile.dw.sorted ))

par(mar=c(2,5,0,.1))
plot(nom.ideal.quantile.dw.sorted[,2],y, axes=F,xlab="", ylab="",  xlim=x.lim ,  xaxs="r", yaxs="r", type="n")
abline(h=y, lty=2, col="gray90")
points(nom.ideal.quantile.dw.sorted[,2],y, pch=19,cex=point.size)
segments(nom.ideal.quantile.dw.sorted[,1], y, nom.ideal.quantile.dw.sorted[,3] , y)

axis(1, at = seq(-.6,.6,.2), 
     labels=c("-.6",  "-.4", "-.2", 0, ".2", ".4", ".6"), mgp=c(2,.5,0), cex.axis=axis.text)
axis(2, at =y, labels=sort.nominee.title.dw, las=1, cex.axis=.8 , mgp=c(2,.5,0))
mtext("Nominee ideal points (NOMINATE),\n(liberal to conservative)", cex=title.text, adj=.1, line=-4)

dev.off()

pdf("Figure_A1_right.pdf", height=8, width=6)

x.lim<-c(-1.5,1.5)

nom.ideal.quantile.bly <- matrix( unlist( tapply(bailey.nomd$nom.proj.sim.bly, bailey.nomd$nominee, function(x) quantile(x, probs=c(lo.ci, .5,.95), na.rm=T)) ), ncol=3, byrow=T)

nom.ideal.quantile.bly.sorted <- nom.ideal.quantile.bly[order( nom.ideal.quantile.bly[,2]),]
sort.nominee.title.bly <- nominee.title.bly[order( nom.ideal.quantile.bly[,2])]
y <- c(1:nrow(nom.ideal.quantile.bly.sorted ))

par(mar=c(2,5,0,.1))
plot(nom.ideal.quantile.bly.sorted[,2],y, axes=F,xlab="", ylab="",  xlim=x.lim ,  xaxs="r", yaxs="r", type="n")
abline(h=y, lty=2, col="gray90")
points(nom.ideal.quantile.bly.sorted[,2],y, pch=19,cex=point.size)
segments(nom.ideal.quantile.bly.sorted[,1], y, nom.ideal.quantile.bly.sorted[,3] , y)

axis(1, at = seq(-1.5,1.5,.2), , mgp=c(2,.5,0), cex.axis=axis.text)
axis(2, at =y, labels=sort.nominee.title.bly, las=1, cex.axis=.8 , mgp=c(2,.5,0))
mtext("A) Nominee ideal points (Bailey), \n(liberal to conservative)", cex=title.text, adj=.1, line=-4)

dev.off()


#plot move the median by nominee using ordered dotplot
#need to take mean of j5new - j5 old, along with CIs, for each nominee

pdf("Figure_A2_left.pdf", height=8, width=6)

move.point <-  tapply( nomd$j5new.sim.dw - nomd$j5old.sim.dw, nomd$nominee, mean)
move.lower.ci <-  tapply( nomd$j5new.sim.dw-nomd$j5old.sim.dw, nomd$nominee, function(x) quantile(x, probs=c(lo.ci)))
move.upper.ci <-  tapply( nomd$j5new.sim.dw-nomd$j5old.sim.dw, nomd$nominee, function(x) quantile(x, probs=c(hi.ci)))

move.sorted <- sort(move.point, decreasing=T)
move.lower.ci.sorted <- move.lower.ci[order(move.point, decreasing=T)]
move.upper.ci.sorted <- move.upper.ci[order(move.point, decreasing=T)]
sort.nominee <- nominee.title.dw[ order(move.point, decreasing=T) ]

par(mar=c(2,5,2,1.5))
y <- c(1:length(move.point))

plot(move.sorted, y, type="n", xlim=c(-.4,.4), xlab="", ylab="", axes=F, xaxs="i", yaxs="r")
abline(h=y, lty=2, col="gray90")
points(move.sorted, y, type="p", pch=19, cex=point.size)
segments(move.lower.ci.sorted , y, move.upper.ci.sorted , y)
axis(1, at = seq(-.4,.4,.1), 
     labels=c("-.4", "-.3","-.2","-.1", 0, ".1",".2",".3", ".4"), mgp=c(2,.5,0))
axis(2, at =y, labels=sort.nominee, las=1, cex.axis=.8 , mgp=c(2,.5,0))
mtext("More liberal",1, adj=.1, line=-3, cex=1.2)
mtext("More conservative",3, adj=1, line=-3, cex=1.2)
mtext("Move-the-Median Impact of Nominees (NOMINATE)",3, font=1, line=0, cex=1.4,xpd=NA, adj=.9)
abline(v=0, col="gray90")

dev.off()


pdf("Figure_A2_right.pdf", height=8, width=6)

move.point <-  tapply( bailey.nomd$j5new.sim.bly - bailey.nomd$j5old.sim.bly, bailey.nomd$nominee, mean)
move.lower.ci <-  tapply( bailey.nomd$j5new.sim.bly-bailey.nomd$j5old.sim.bly, bailey.nomd$nominee, function(x) quantile(x, probs=c(lo.ci)))
move.upper.ci <-  tapply( bailey.nomd$j5new.sim.bly-bailey.nomd$j5old.sim.bly, bailey.nomd$nominee, function(x) quantile(x, probs=c(hi.ci)))

move.sorted <- sort(move.point, decreasing=T)
move.lower.ci.sorted <- move.lower.ci[order(move.point, decreasing=T)]
move.upper.ci.sorted <- move.upper.ci[order(move.point, decreasing=T)]
sort.nominee <- nominee.title.bly[ order(move.point, decreasing=T) ]

par(mar=c(2,5,2,1.5))
y <- c(1:length(move.point))

plot(move.sorted, y, type="n", xlim=c(-.6,.6), xlab="", ylab="", axes=F, xaxs="i", yaxs="r")
abline(h=y, lty=2, col="gray90")
points(move.sorted, y, type="p", pch=19, cex=point.size)
segments(move.lower.ci.sorted , y, move.upper.ci.sorted , y)
axis(1, at = seq(-.4,.4,.1), 
     labels=c("-.4", "-.3","-.2","-.1", 0, ".1",".2",".3", ".4"), mgp=c(2,.5,0))
axis(2, at =y, labels=sort.nominee, las=1, cex.axis=.8 , mgp=c(2,.5,0))
mtext("More liberal",1, adj=.1, line=-3, cex=1.2)
mtext("More conservative",3, adj=1, line=-3, cex=1.2)
mtext("Move-the-Median Impact of Nominees (Bailey)",3, font=1, line=0, cex=1.4)
abline(v=0, col="gray90")

dev.off()


